﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
#region Version Info
/* ========================================================================
* 【本页面功能概述】
*
* 作者：wangjianfeng 创建时间：2012/5/24 15:28:17
* 文件名：SerializeHelpercs.cs
*
* 修改者： 时间：
* 修改说明：
* ========================================================================
*/
#endregion
namespace Utils.Common
{
    /// <summary>
    /// 序例化对象
    /// </summary>
    public class SerializeHelpercs
    {
        ///<summary>
        /// 序列化 对象到字符串
        ///</summary>
        ///<param name="obj">泛型对象</param>
        ///<returns>序列化后的字符串</returns>
        public static string Serialize<T>(T obj)
        {
            try
            {
                IFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream();
                formatter.Serialize(stream, obj);
                stream.Position = 0;
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Flush();
                stream.Close();
                stream.Dispose();
                return Convert.ToBase64String(buffer);
            }
            catch (Exception ex)
            {
                throw new Exception("序列化失败,原因:" + ex.Message);
            }
        }

        ///<summary>
        /// 反序列化 字符串到对象
        ///</summary>
        ///<param name="obj">泛型对象</param>
        ///<param name="str">要转换为对象的字符串</param>
        ///<returns>反序列化出来的对象</returns>
        public static T Desrialize<T>(T obj, string str)
        {
            try
            {
                obj = default(T);
                IFormatter formatter = new BinaryFormatter();
                byte[] buffer = Convert.FromBase64String(str);
                MemoryStream stream = new MemoryStream(buffer);
                obj = (T)formatter.Deserialize(stream);
                stream.Flush();
                stream.Close();
                stream.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("反序列化失败,原因:" + ex.Message);
            }
            return obj;
        }
    }
}
